const config = require('./config')

// 后台请求代理使用express
const express = require('express')
const app = express()
let apiRoutes = express.Router()
app.use('/api', apiRoutes)

// 引入axios发送XHR
const axios = require('axios')
app.get('/api/getDiscList', (req, resp) => {
  let url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
  axios
    .get(url, {
      headers: {
        Origin: 'https://y.qq.com',
        Referer: 'https://y.qq.com/portal/playlist.html'
      },
      params: req.query
    })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})
app.get('/api/_getLyric', (req, resp) => {
  const url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
  axios
    .get(url, {
      headers: {
        Accept: 'application/json, text/javascript, */*; q=0.01',
        Origin: 'https://y.qq.com',
        Referer: 'https://y.qq.com/portal/player.html'
      },
      params: req.query
    })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})
app.get('/api/getDisc', (req, resp) => {
  const url =
    'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
  const disstid = req.query.disstid
  axios
    .get(url, {
      headers: {
        Origin: 'https://y.qq.com',
        Referer: `https://y.qq.com/n/yqq/playsquare/${disstid}.html`
      },
      params: req.query
    })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})
app.get('/api/getToplist', (req, resp) => {
  const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
  const params = {
    _: new Date().getTime()
  }
  const data = {
    req_0: {
      module: 'musicToplist.ToplistInfoServer',
      method: 'GetAll',
      param: {}
    },
    comm: {
      g_tk: 709943229,
      uin: 0,
      format: 'json',
      ct: 23,
      cv: 0
    }
  }
  axios({
    url: url,
    method: 'POST',
    headers: {'content-type': 'multipart/form-data'},
    params: params,
    data: data
  })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})
app.get('/api/getMusicList', (req, resp) => {
  const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
  const headers = {
    Origin: 'https://y.qq.com',
    Referer: 'https://y.qq.com/n/yqq/toplist/4.html'
  }
  axios
    .get(url, {
      headers,
      params: req.query
    })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})
app.get('/api/search', (req, resp) => {
  const url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
  const headers = {
    Origin: 'https://y.qq.com',
    Referer: 'https://y.qq.com/m/index.html'
  }
  axios
    .get(url, {
      headers,
      params: req.query
    })
    .then(response => {
      resp.json(response.data)
    })
    .catch(e => {
      console.log(e.message)
    })
})

app.use(express.static('./dist'))

let port = process.env.PORT || config.build.port

module.exports = app.listen(port, function (err) {
  if (err) {
    console.log(err)
    return
  }
  console.log('Listening at http://localhost:' + port + '\n')
})
